package us.wili.dev.platform.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import us.wili.dev.platform.service.PlatformFileService;
import us.wili.dev.platform.util.QiniuUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class PlatformFileServiceImpl implements PlatformFileService {
    private static final Logger logger = Logger.getLogger(PlatformFileServiceImpl.class);

    @Override
    public Map<String, List<String>> uploadImgs(MultipartFile[] file) throws IOException {
        Map<String, List<String>> resultMap = new HashMap<>();
        List<String> list = new LinkedList<>();
        String result = null;

        for (int i = 0; i < file.length; i++) {
            String fileName = file[i].getOriginalFilename();

            FileInputStream inputStream = (FileInputStream) file[0].getInputStream();

            QiniuUtil qn = new QiniuUtil();

            try {

                result = qn.uoloapQiniu(inputStream,fileName);

                if (StringUtils.isNotBlank(result)) {
                    list.add(result);
                }
            }catch (Exception ex){
                System.out.printf(ex.getMessage());
            }
        }
        logger.info("imagesList == " + list);
        if (list.isEmpty()) {
            list.add("error");
        }
        resultMap.put("result",list);
        return resultMap;
    }

    @Override
    public String uptoken() {
        QiniuUtil qn = new QiniuUtil();

        return qn.uptoken();
    }
}
